﻿using LibraryManagementSystem.Models.Enum;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

namespace LibraryManagementSystem.Models;

public partial class LibraryContext : DbContext
{
    public LibraryContext()
    {
    }

    public LibraryContext(DbContextOptions<LibraryContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Borrowrecord> Borrowrecords { get; set; }

    public virtual DbSet<Category> Categories { get; set; }

    public virtual DbSet<Libraryitem> Libraryitems { get; set; }

    public virtual DbSet<Member> Members { get; set; }

    public virtual DbSet<Reservation> Reservations { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
        => optionsBuilder.UseMySQL("server=localhost;uid=root;pwd=root;database=library");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Borrowrecord>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("borrowrecords", tb => tb.HasComment("借阅记录表"));

            entity.HasIndex(e => e.LibraryItemId, "idx_borrowrecord_libraryitem");

            entity.HasIndex(e => e.MemberId, "idx_borrowrecord_member");

            entity.Property(e => e.Id).HasComment("借阅记录ID，主键，自增");
            entity.Property(e => e.BorrowDate)
                .HasComment("借阅日期")
                .HasColumnType("date");
            entity.Property(e => e.DueDate)
                .HasComment("应还日期")
                .HasColumnType("date");
            entity.Property(e => e.Fine)
                .HasPrecision(8)
                .HasComment("罚款金额");
            entity.Property(e => e.LibraryItemId).HasComment("文献ID，外键");
            entity.Property(e => e.MemberId).HasComment("会员ID，外键");
            entity.Property(e => e.ReturnDate)
                .HasComment("归还日期")
                .HasColumnType("date");
            entity.Property(e => e.Status)
                .HasDefaultValueSql("'Borrowed'")
                .HasComment("借阅状态")
                .HasColumnType("enum('Borrowed','Returned','Overdue')");

            entity.HasOne(d => d.LibraryItem).WithMany(p => p.Borrowrecords)
                .HasForeignKey(d => d.LibraryItemId)
                .HasConstraintName("fk_borrowrecord_libraryitem");

            entity.HasOne(d => d.Member).WithMany(p => p.Borrowrecords)
                .HasForeignKey(d => d.MemberId)
                .HasConstraintName("fk_borrowrecord_member");
        });

        modelBuilder.Entity<Category>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("categorys", tb => tb.HasComment("图书分类表"));

            entity.HasIndex(e => e.Name, "uq_category_name").IsUnique();

            entity.Property(e => e.Id).HasComment("分类ID，主键，自增");
            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasComment("分类名称");
        });

        // 配置Libraryitem的公共属性
        modelBuilder.Entity<Libraryitem>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");
            entity.Property(e => e.Id).HasComment("文献ID，主键，自增");
            entity.Property(e => e.Title)
                .HasMaxLength(100)
                .HasComment("标题");
            entity.Property(e => e.Author)
                .HasMaxLength(50)
                .HasComment("作者");
            entity.Property(e => e.CategoryId).HasComment("分类ID，外键");
            entity.Property(e => e.Price)
                .HasPrecision(10)
                .HasComment("价格");
            entity.Property(e => e.TotalCopies)
                .HasDefaultValueSql("'1'")
                .HasComment("总副本数");
            entity.Property(e => e.AvailableCopies)
                .HasDefaultValueSql("'1'")
                .HasComment("可用副本数");
            entity.Property(e => e.Type)
                .HasComment("文献类型")
                .HasColumnType("enum('Book','Magazine')");
        });

        // 配置Book的特有属性
        modelBuilder.Entity<Book>(entity =>
        {
            entity.Property(e => e.Isbn)
                .HasMaxLength(20)
                .HasComment("ISBN编号，图书专用")
                .HasColumnName("ISBN");
            entity.Property(e => e.Publisher)
                .HasMaxLength(50)
                .HasComment("出版社，图书专用");
        });

        // 配置Magazine的特有属性
        modelBuilder.Entity<Magazine>(entity =>
        {
            entity.Property(e => e.Issn)
                .HasMaxLength(20)
                .HasComment("ISSN编号，杂志专用")
                .HasColumnName("ISSN");
            entity.Property(e => e.IssueNumber)
                .HasComment("期号，杂志专用");
        });

        modelBuilder.Entity<Member>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("members", tb => tb.HasComment("会员信息表"));

            entity.HasIndex(e => e.Username, "uq_member_username").IsUnique();

            entity.Property(e => e.Id).HasComment("会员ID，主键，自增");
            entity.Property(e => e.Contact)
                .HasMaxLength(50)
                .HasComment("联系方式");
            entity.Property(e => e.MemberType)
                .HasDefaultValueSql("'Student'")
                .HasComment("会员类型")
                .HasColumnType("enum('Student','Staff','VIP')");
            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasComment("会员姓名");
            entity.Property(e => e.Password)
                .HasMaxLength(100)
                .HasComment("会员密码");
            entity.Property(e => e.Username)
                .HasMaxLength(50)
                .HasComment("会员登录名，唯一");
        });

        modelBuilder.Entity<Reservation>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("reservations", tb => tb.HasComment("图书预约表"));

            entity.HasIndex(e => e.LibraryItemId, "idx_reservation_libraryitem");

            entity.HasIndex(e => e.MemberId, "idx_reservation_member");

            entity.Property(e => e.Id).HasComment("预约ID，主键，自增");
            entity.Property(e => e.LibraryItemId).HasComment("文献ID，外键");
            entity.Property(e => e.MemberId).HasComment("会员ID，外键");
            entity.Property(e => e.ReserveDate)
                .HasComment("预约日期")
                .HasColumnType("date");
            entity.Property(e => e.Status)
                .HasDefaultValueSql("'Active'")
                .HasComment("预约状态")
                .HasColumnType("enum('Active','Cancelled','Fulfilled')");

            entity.HasOne(d => d.LibraryItem).WithMany(p => p.Reservations)
                .HasForeignKey(d => d.LibraryItemId)
                .HasConstraintName("fk_reservation_libraryitem");

            entity.HasOne(d => d.Member).WithMany(p => p.Reservations)
                .HasForeignKey(d => d.MemberId)
                .HasConstraintName("fk_reservation_member");
        });
        // 配置 Discriminator 列
        modelBuilder.Entity<Libraryitem>()
            .HasDiscriminator<BookCategory>("Type")
            .HasValue<Book>(BookCategory.Book)
            .HasValue<Magazine>(BookCategory.Magazine);
        OnModelCreatingPartial(modelBuilder);

    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
